package com.hengpeng.common.config;

import java.util.HashMap;
import java.util.Properties;

import com.hengpeng.common.config.expander.PropertiesExpander;
import com.hengpeng.common.config.monitor.CheckCallBack;

/**
 * <pre>
 * 配置文件管理类
 * </pre>
 * @author chenjian
 * @version 1.0, 2010-12-22
 */
public class ConfigManager
{
	public static HashMap<String, ConfigProvider<?>> cache_configs = new HashMap<String, ConfigProvider<?>>();

	public static <T extends Config, K> void setXmlConfig(String key, String xmlSource, CheckCallBack<T, K> call,
			Class<T> clz)
	{
		ConfigProvider<K> configProvider = (ConfigProvider<K>) new XmlProvider<T, K>(xmlSource, call, clz);
		cache_configs.put(key, configProvider);
	}

	public static <T extends Config, K> void setXmlPathConfig(String key, String xmlPath, Boolean isListenerChange,
			CheckCallBack<T, K> call, Class<T> clz)
	{
		ConfigProvider<K> configProvider = (ConfigProvider<K>) new XmlProvider<T, K>(xmlPath, isListenerChange, call,
				clz);
		cache_configs.put(key, configProvider);
	}

	public static void setPropertiesConfig(String key, String propertiesSource)
	{
		ConfigProvider<Properties> configProvider = new PropertiesProvider(propertiesSource);
		cache_configs.put(key, configProvider);
	}

	public static void setPropertiesPathConfig(String key, String propertiesPath, Boolean isListenerChange)
	{
		ConfigProvider<Properties> configProvider = new PropertiesProvider(propertiesPath, isListenerChange);
		cache_configs.put(key, configProvider);
	}

	public static Object getConfig(String key)
	{
		return cache_configs.get(key);
	}

	@SuppressWarnings("unchecked")
	public static <T> ConfigProvider<T> getXmlConfig(String key, Class<T> clz)
	{
		return (ConfigProvider<T>) cache_configs.get(key);
	}

	public static PropertiesExpander getPropertiesConfig(String key)
	{
		return (PropertiesExpander) cache_configs.get(key);
	}
}
